package com.bang.study.jvm.demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: HeapOOM
 * @author: BANG
 * @Date: 2019/10/30 17:43
 * @Version: 1.0
 * @Modified By:
 * @Desc: 模拟堆内存溢出
 */
public class HeapOOM {

    /**
     * JVM参数:
     * -Xms10m -Xmx10m -XX:+PrintGCDetails -Xloggc:heap-gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ -XX:+UseParNewGC -XX:+UseConcMarkSweepGC
     *
     * @param args
     */
    public static void main(String[] args) {
        long count = 0;
        List<Object> list = new ArrayList<>();
        while (true) {
            list.add(new MyObject());
            System.out.println("当前创建了" + (++count) + "个对象");
        }
    }


}

class MyObject {

    private String name;

    private Integer age;

    private byte[] mb_1 = new byte[1024];

    public MyObject() {
    }

    public MyObject(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyObject myObject = (MyObject) o;
        return Objects.equals(name, myObject.name) &&
                Objects.equals(age, myObject.age);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "MyObject{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
